package net.peihuan.service.impl;

import net.peihuan.dao.ArticleMapper;
import net.peihuan.dao.ArticleTagMapper;
import net.peihuan.entity.Article;
import net.peihuan.service.ArticleService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private ArticleTagMapper articleTagMapper;

    @Override
    public int save(Article article) {
        return articleMapper.insert(article);
    }

    @Override
    public boolean update(Article article) {
        return articleMapper.updateByPrimaryKeySelective(article)==1;
    }

    @Override
    public boolean delete(int id) {
        try {
            articleTagMapper.deleteByArticleId(id);
            int count= articleMapper.deleteByPrimaryKey(id);
            return count==1;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public Article findById(int id) {
        Article article= articleMapper.selectByPrimaryKey(id);
        if(article!=null)
            article.setDigest(getDigest(article.getContent())); //设置 摘要
        return article;
    }

    @Override
    public List<Article> findAllWithDetails(int beginIndex, int offset) {
        Map<String, Object> data = new HashedMap();
        data.put("beginIndex",beginIndex);
        data.put("offset", offset);

        List<Article> articles= articleMapper.selectAllArticlesWithTags(data);
        getArticleDigest(articles);         //设置摘要
        return articles;
    }

    @Override
    public List<Article> findAllBaseResultMap(int beginIndex, int offset) {
        Map<String, Object> data = new HashedMap();
        data.put("beginIndex",beginIndex);
        data.put("offset", offset);

        List<Article> articles= articleMapper.selectAllArticlesBaseResultMap(data);
        getArticleDigest(articles);         //设置摘要
        return articles;
    }

    @Override
    public List<Article> findAllBaseResultMap() {
        return articleMapper.selectAllArticlesBaseResultMap2();
    }

    @Override
    public List<Article> findByCategoryAlias(String alias) {
        return articleMapper.selectByCategoryAlias(alias);
    }

    @Override
    public List<Article> findByTagAlias(String alias) {
        return articleMapper.selectByTagAlias(alias);
    }

    @Override
    public List<Article> findNearby(String time) {
        Article older=articleMapper.selectOlder(time);
        Article newer=articleMapper.selectNewer(time);

        List<Article> articles=new ArrayList<>();
        articles.add(newer);
        articles.add(older);

        return articles;
    }

    @Override
    public int countArticles() {
        return articleMapper.countArticles();
    }


    //根据文章内容筛选出摘要 ,第一个文字 <p>
    private void getArticleDigest(List<Article> articles){
        for(Article article:articles){
            article.setDigest(getDigest(article.getContent()));
        }
    }
    private String getDigest(String content){
        //正则筛选第一个文字<p>
        Pattern datePattern = Pattern.compile("<p>((?!<img).)*?<\\/p>");
        Matcher dateMatcher = datePattern.matcher(content);
        if(dateMatcher.find()) {
            return dateMatcher.group();   //返回第一个<p>
        }else
        {
            return "null";
        }
    }
}
